import 'package:aqua_plunge/constants/constants.dart';
import 'package:aqua_plunge/cover/LogPvManager.dart';
import 'package:aqua_plunge/dao/login_dao.dart';
import 'package:aqua_plunge/page/tab_navigation_page.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../http/app_manager.dart';
import '../util/HiCache.dart';
import '../widget/GetMaterialAppWidget.dart';
import 'lead_page.dart';

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {

    LogPvManager.stat(page: Constants.pageLaunch);
    return FutureBuilder<bool>(
        future: _doInit(),
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          var widget = snapshot.connectionState == ConnectionState.done
              ? snapshot.data == false
                  ? const LeadPage()
                  : const TabNavigationPage()
              : const Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
          return GetMaterialAppWidget(child: widget);
        });
  }

  Future<bool> _doInit() async {
    await HiCache.preInit();
    var prefs = await SharedPreferences.getInstance();
    return await LoginDao.checkToken(prefs.getString('token')??'');
  }
}
